#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>

#define NUM 1024
#define SIZE 32
#define SEP " "

//保存完整的命令行字符串
char cmd_line[NUM];
//保存打散之后的命令行字符串
char* g_argv[SIZE];

// shell 运行原理 ： 通过让子进程执行命令，父进程等待&&解析命令
int main()
{
	//0. 命令行解释器，一定是一个常驻内存的进程，不退出
	while (1)
	{
		//1. 打印出提示信息 [whb@localhost myshell]# 
		printf("[root@localhost myshell]# ");
		fflush(stdout);
		memset(cmd_line, '\0', sizeof cmd_line);
		//2. 获取用户的键盘输入[输入的是各种指令和选项: "ls -a -l -i"]
		if (fgets(cmd_line, sizeof cmd_line, stdin) == NULL)
		{
			continue;
		}
	

    cmd_line[strlen(cmd_line)-1] = '\0';
	//3.命令字符串解析
	g_argv[0] = strtok(cmd_line, SEP);
	int index = 1;
	while (g_argv[index++] = strtok(NULL, SEP));
    //4.TODO
    if(strcmp(g_argv[0],"cd") == 0)
    {
        if(g_argv[1] != NULL)
        {
            chdir(g_argv[1]);
        }
    }
	//5. fork()
	pid_t id = fork();
	if (id == 0) //child
	{
		printf("下面功能让子进程进行的\n");
		//cd cmd , current child path
		execvp(g_argv[0], g_argv); // ls -a -l -i
		exit(1);
	}
	//father
	int status = 0;
	pid_t ret = waitpid(id, &status, 0);
	if (ret > 0) printf("exit code: %d\n", WEXITSTATUS(status));

    
    }
}
